package com.dferreira.axil.data;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import com.dferreira.axil.model.Categoria;

@RequestScoped
public class ListaDeCategorias {

    @Inject
    private RepositorioDeCategorias repositorio;

    private List<Categoria> categorias;

    @Produces
    @Named
    public List<Categoria> getCategorias() {
        return categorias;
    }

    public void quandoAlterarListaDeCategorias(@Observes(notifyObserver = Reception.IF_EXISTS) final Categoria categoria) {
    	recuperarCategorias();
    }

    @PostConstruct
    public void recuperarCategorias() {
        categorias = repositorio.findAllOrderedByName();
    }
}
